home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Archives / GNU / GNUPLOTsrc.lha / term / png.trm < prev    next >
Encoding:
Text File  |  1996-01-22  |  9.4 KB  |  404 lines

  1. /*
  2.  * $Id: png.trm,v 1.2 1995/12/20 21:48:05 drd Exp $
  3.  *
  4.  */
  5.  
  6. /* GNUPLOT - png.trm */
  7. /*
  8.  * Copyright (C) 1995
  9.  *
  10.  * Permission to use, copy, and distribute this software and its
  11.  * documentation for any purpose with or without fee is hereby granted,
  12.  * provided that the above copyright notice appear in all copies and
  13.  * that both that copyright notice and this permission notice appear
  14.  * in supporting documentation.
  15.  *
  16.  * Permission to modify the software is granted, but not the right to
  17.  * distribute the modified code.  Modifications are to be distributed
  18.  * as patches to released version.
  19.  *
  20.  * This software  is provided "as is" without express or implied warranty.
  21.  *
  22.  * This file is included by ../term.c.
  23.  *
  24.  * This terminal driver supports:
  25.  *  png
  26.  *
  27.  * AUTHORS
  28.  *  Alexander Lehmann
  29.  *                      derived from pbm.trm by Russell Lang
  30.  *
  31.  * send your comments or suggestions to (info-gnuplot@dartmouth.edu).
  32.  *
  33.  */
  34.  
  35. /* To compile this terminal driver, you need libpng and zlib, both are
  36.    available at ftp://ftp.uu.net/graphics/png
  37. */
  38.  
  39. /* The following pbmplus drivers use the generic bit mapped graphics
  40.    routines from bitmap.c to build up a bit map in memory.  The driver
  41.    interchanges colomns and lines in order to access entire lines
  42.    easily and returns the lines to get bits in the right order :
  43.    (x,y) -> (y,XMAX-1-x). */
  44. /* This interchange is done by calling b_makebitmap() with reversed
  45.    xmax and ymax, and then setting b_rastermode to TRUE.  b_setpixel()
  46.    will then perform the interchange before each pixel is plotted */
  47.  
  48. #ifndef GOT_DRIVER_H
  49. #include "driver.h"
  50. #endif
  51.  
  52. #ifdef TERM_REGISTER
  53. register_term(png_driver)
  54. #endif
  55.  
  56. #ifdef TERM_PROTO
  57. TERM_PUBLIC void PNGoptions __P((void));
  58. TERM_PUBLIC void PNGinit __P((void));
  59. TERM_PUBLIC void PNGreset __P((void));
  60. TERM_PUBLIC void PNGsetfont __P((void));
  61. TERM_PUBLIC void PNGgraphics __P((void));
  62. TERM_PUBLIC void PNGtext __P((void));
  63. TERM_PUBLIC void PNGlinetype __P((int linetype));
  64. TERM_PUBLIC void PNGpoint __P((unsigned int x, unsigned int y, int point));
  65. #endif /* TERM_PROTO */
  66.  
  67. #ifdef TERM_BODY
  68.  
  69. #include "png.h"
  70.  
  71. /* make XMAX and YMAX a multiple of 8 */
  72. #define PNG_XMAX (640)
  73. #define PNG_YMAX (480)
  74. #define PNG_VCHAR (FNT5X9_VCHAR)
  75. #define PNG_HCHAR (FNT5X9_VCHAR)
  76. #define PNG_VTIC FNT5X9_HBITS
  77. #define PNG_HTIC FNT5X9_HBITS
  78.  
  79. static int png_font=1;  /* small font */
  80. static int png_mode=0;  /* 0:monochrome 1:gray 2:color */
  81.  
  82. /* 7=black, 0=white */
  83. static int png_gray[]={7,1,6,5,4,3,2,1,7};  /* grays  */
  84. /* bit3=!intensify, bit2=!red, bit1=!green, bit0=!blue */
  85. static int png_color_table[]={15,8,3,5,6,4,2,1,11,13,14}; /* colors */
  86. static png_color png_palette[16];
  87.  
  88. void PNGoptions()
  89. {
  90.   png_font=1;
  91.   png_mode=0;
  92.  
  93.   term_options[0]='\0';
  94.  
  95.   while (!END_OF_COMMAND) {
  96.     if (almost_equals(c_token,"s$mall"))
  97.       png_font=1;
  98.     else if (almost_equals(c_token,"me$dium"))
  99.       png_font=2;
  100.     else if (almost_equals(c_token,"l$arge"))
  101.       png_font=3;
  102.     else if (almost_equals(c_token,"mo$nochrome"))
  103.       png_mode=0;
  104.     else if (almost_equals(c_token,"g$ray"))
  105.       png_mode=1;
  106.     else if (almost_equals(c_token,"c$olor"))
  107.       png_mode=2;
  108.     else {
  109.       png_font=1; /* reset to default, since term is already set */
  110.       png_mode=0;
  111.       int_error("expecting: {small, medium, large} and {monochrome, gray, color}",c_token);
  112.     }
  113.     c_token++;
  114.   }
  115.  
  116.   /* setup options string */
  117.  
  118.   switch(png_font) {
  119.     case 1: strcat(term_options,"small"); break;
  120.     case 2: strcat(term_options,"medium"); break;
  121.     case 3: strcat(term_options,"large"); break;
  122.   }
  123.  
  124.   switch(png_mode) {
  125.     case 0: strcat(term_options," monochrome"); break;
  126.     case 1: strcat(term_options," gray"); break;
  127.     case 2: strcat(term_options," color"); break;
  128.   }
  129. }
  130.  
  131.  
  132. void PNGinit()
  133. {
  134. #ifdef REOPEN_BINARY
  135.    reopen_binary();
  136. #endif /* REOPEN_BINARY */
  137. }
  138.  
  139.  
  140. void PNGreset()
  141. {
  142. #ifdef vms
  143.    fflush_binary();
  144. #endif /* vms */
  145. }
  146.  
  147.  
  148. void PNGsetfont()
  149. {
  150.   switch(png_font) {
  151.     case 1:
  152.       b_charsize(FNT5X9);
  153.       term->v_char = FNT5X9_VCHAR;
  154.       term->h_char = FNT5X9_HCHAR;
  155.       term->v_tic = FNT5X9_HBITS;
  156.       term->h_tic = FNT5X9_HBITS;
  157.       break;
  158.     case 2:
  159.       b_charsize(FNT9X17);
  160.       term->v_char = FNT9X17_VCHAR;
  161.       term->h_char = FNT9X17_HCHAR;
  162.       term->v_tic = FNT9X17_HBITS;
  163.       term->h_tic = FNT9X17_HBITS;
  164.       break;
  165.     case 3:
  166.       b_charsize(FNT13X25);
  167.       term->v_char = FNT13X25_VCHAR;
  168.       term->h_char = FNT13X25_HCHAR;
  169.       term->v_tic = FNT13X25_HBITS;
  170.       term->h_tic = FNT13X25_HBITS;
  171.       break;
  172.     }
  173. }
  174.  
  175.  
  176. void PNGgraphics()
  177. {
  178.   int numplanes;
  179.  
  180.   switch(png_mode) {
  181.     case 0: numplanes=1; break;
  182.     case 1: numplanes=3; break;
  183.     case 2: numplanes=4; break;
  184.   }
  185.  
  186.   PNGsetfont();
  187.   /* rotate plot -90 degrees by reversing XMAX and YMAX and by
  188.      setting b_rastermode to TRUE */
  189.   b_makebitmap((unsigned int)(PNG_YMAX*ysize),
  190.                (unsigned int)(PNG_XMAX*xsize),numplanes);
  191.   b_rastermode = TRUE;
  192.  
  193.   if(png_mode!=0)
  194.     b_setlinetype(0); /* solid lines */
  195. }
  196.  
  197. void PNGtext()
  198. {
  199.   register int x,j,row;
  200.   png_struct *png_ptr;
  201.   png_info *info_ptr;
  202.   register int i,value;
  203.   png_bytef *prow;
  204.   int mask, plane1, plane2, plane3, plane4;
  205.   int red, green, blue;
  206.   png_text pngtext;
  207.   char text[100];
  208.   extern char version[];
  209.   extern char patchlevel[];
  210.  
  211.   png_ptr = malloc(sizeof (png_struct));
  212.   if (!png_ptr) {
  213.     b_freebitmap();
  214.     return;
  215.   }
  216.   info_ptr = malloc(sizeof (png_info));
  217.   if (!info_ptr) {
  218.     free(png_ptr);
  219.     b_freebitmap();
  220.     return;
  221.   }
  222.   prow=malloc(b_ysize);
  223.   if(!prow) {
  224.     free(png_ptr);
  225.     free(info_ptr);
  226.     b_freebitmap();
  227.     return;
  228.   }
  229.  
  230.   if (setjmp(png_ptr->jmpbuf)) {
  231.     png_write_destroy(png_ptr);
  232.     free(png_ptr);
  233.     free(info_ptr);
  234.     free(prow);
  235.     b_freebitmap();
  236.     return;
  237.   }
  238.  
  239.   png_info_init(info_ptr);
  240.   png_write_init(png_ptr);
  241.  
  242.   png_init_io(png_ptr, outfile);
  243.  
  244.   info_ptr->width=b_ysize;
  245.   info_ptr->height=b_xsize;
  246.  
  247.   info_ptr->bit_depth=png_mode==0 ? 1 : 4;
  248.   info_ptr->color_type=png_mode==2 ? PNG_COLOR_TYPE_PALETTE : PNG_COLOR_TYPE_GRAY;
  249.   if(png_mode==2) {
  250.     info_ptr->valid |= PNG_INFO_PLTE;
  251.     info_ptr->palette=png_palette;
  252.     info_ptr->num_palette=16;
  253.   }
  254.   if(png_mode!=0) {
  255.     info_ptr->valid |= PNG_INFO_sBIT;
  256.     if(png_mode==1) {
  257.       info_ptr->sig_bit.gray = 3;
  258.       png_set_shift(png_ptr, &(info_ptr->sig_bit));
  259.     } else {
  260.       info_ptr->sig_bit.red = 2;
  261.       info_ptr->sig_bit.green = 2;
  262.       info_ptr->sig_bit.blue = 2;
  263.     }
  264.   }
  265.   info_ptr->interlace_type=0;
  266.   if(png_mode==0)
  267.     png_set_invert_mono(png_ptr);
  268.  
  269.   if(png_mode==2)
  270.     for(i=0;i<16;i++) {
  271.       red = (i&4) ? 1 : 3;
  272.       green = (i&2) ? 1 : 3;
  273.       blue = (i&1) ? 1 : 3;
  274.       if (i&8) {
  275.         red--; green--; blue--;
  276.       }
  277.       png_palette[i].red=red*85;
  278.       png_palette[i].green=green*85;
  279.       png_palette[i].blue=blue*85;
  280.     }
  281.  
  282.   sprintf(text, "gnuplot %sversion %s patchlevel %s", OS, version, patchlevel);
  283.  
  284.   pngtext.compression=-1;
  285.   pngtext.key="Software";
  286.   pngtext.text=text;
  287.   pngtext.text_length=strlen(text);
  288.  
  289.   info_ptr->num_text=1;
  290.   info_ptr->text=&pngtext;
  291.  
  292.   png_write_info(png_ptr, info_ptr);
  293.  
  294.   info_ptr->num_text=0;
  295.   info_ptr->text=NULL;
  296.  
  297.   png_set_packing(png_ptr);
  298.  
  299.   /* dump bitmap in raster mode */
  300.   for (x = b_xsize-1; x >= 0; x--) {
  301.     row = (b_ysize/8)-1;
  302.     for (j = row; j >= 0; j--) {
  303.       mask = 0x80;
  304.       plane1=(*((*b_p)[j]+x));
  305.       if(png_mode!=0) {
  306.         plane2=(*((*b_p)[j+b_psize]+x));
  307.         plane3=(*((*b_p)[j+b_psize+b_psize]+x));
  308.       } else {
  309.         plane2=0;
  310.         plane3=0;
  311.       }
  312.       if(png_mode==2)
  313.         plane4=(*((*b_p)[j+b_psize+b_psize+b_psize]+x));
  314.       else
  315.         plane4=0;
  316.  
  317.       for (i=0; i<8; i++) {
  318.         value=0;
  319.         if (plane1 & mask)  value+=1;
  320.         if (plane2 & mask)  value+=2;
  321.         if (plane3 & mask)  value+=4;
  322.         if (plane4 & mask)  value+=8;
  323.         if(png_mode==1) value=7-value;
  324.  
  325.         prow[(row-j)*8+i]=(png_byte)value;
  326.         mask>>=1;
  327.       }
  328.     }
  329.     png_write_rows(png_ptr, &prow, 1);
  330.   }
  331.  
  332.   png_write_end(png_ptr, info_ptr);
  333.   png_write_destroy(png_ptr);
  334.  
  335.   free(png_ptr);
  336.   free(info_ptr);
  337.   free(prow);
  338.   b_freebitmap();
  339. }
  340.  
  341. void PNGlinetype(linetype)
  342. int linetype;
  343. {
  344.   switch(png_mode) {
  345.     case 0:
  346.       b_setlinetype(linetype);
  347.       break;
  348.     case 1:
  349.       if (linetype>=7)
  350.         linetype %= 7;
  351.       b_setvalue(png_gray[linetype+2]);
  352.       break;
  353.     case 2:
  354.       if (linetype>=9)
  355.         linetype %= 9;
  356.       b_setvalue(png_color_table[linetype+2]);
  357.       break;
  358.   }
  359. }
  360.  
  361. void PNGpoint(x,y,point)
  362. unsigned int x,y;
  363. int point;
  364. {
  365.   if(png_mode==0) line_and_point(x,y,point);
  366.   else            do_point(x,y,point);
  367. }
  368.  
  369. #endif /* TERM_BODY */
  370.  
  371. #ifdef TERM_TABLE
  372.  
  373. TERM_TABLE_START(png_driver)
  374.       "png", "Portable Network Graphics [small medium large] [monochrome gray color]",
  375.        PNG_XMAX, PNG_YMAX, PNG_VCHAR,
  376.        PNG_HCHAR, PNG_VTIC, PNG_HTIC, PNGoptions,
  377.        PNGinit, PNGreset, PNGtext, null_scale,
  378.        PNGgraphics, b_move, b_vector, PNGlinetype,
  379.        b_put_text, b_text_angle, null_justify_text, PNGpoint,
  380.        do_arrow, set_font_null,
  381.                         0, /* pointsize */
  382.                         TERM_CAN_MULTIPLOT
  383. TERM_TABLE_END(png_driver)
  384.  
  385. #undef LAST_TERM
  386. #define LAST_TERM png_driver
  387.  
  388. #endif /* TERM_TABLE */
  389.  
  390. /*
  391.  * NAME: png
  392.  *
  393.  * OPTIONS: small|medium|large (default small) <-- fontsize
  394.  *        monochrome|gray|colour (default monochrome)
  395.  *
  396.  * SUPPORTS: Portable Network Graphics 
  397.  *
  398.  * Further Info: Needs third party library:
  399.  *
  400.  * To compile this terminal driver, you need libpng and zlib, both are
  401.  *  available at ftp://ftp.uu.net/graphics/png
  402.  *
  403.  */
  404.